home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / scripts / polynomial / roots.m < prev   
Text File  |  1996-07-15  |  2KB  |  63 lines

  1. ## Copyright (C) 1996 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## usage: roots (v)
  21. ##
  22. ## For a vector v with n components, return the roots of the
  23. ## polynomial v(1) * z^(n-1) + ... + v(n-1) * z + v(n).
  24.  
  25. ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at>
  26. ## Created: 24 December 1993
  27. ## Adapted-By: jwe
  28.  
  29. function r = roots (v)
  30.  
  31.   if (min (size (v)) > 1 || nargin != 1)
  32.     usage ("roots (v), where v is a vector");
  33.   endif
  34.  
  35.   n = length (v);
  36.   v = reshape (v, 1, n);
  37.  
  38.   ## If v = [ 0 ... 0 v(k+1) ... v(k+l) 0 ... 0 ], we can remove the
  39.   ## leading k zeros and n - k - l roots of the polynomial are zero.
  40.  
  41.   f = find (v);
  42.   m = max (size (f));
  43.  
  44.   if (m > 0 && n > 1)
  45.     v = v(f(1):f(m));
  46.     l = max (size (v));
  47.     if (l > 1)
  48.       A = diag (ones (1, l-2), -1);
  49.       A(1,:) = -v(2:l) ./ v(1);
  50.       r = eig (A);
  51.       if (f(m) < n)
  52.         tmp = zeros (n - f(m), 1);
  53.     r = [r; tmp];
  54.       endif
  55.     else
  56.       r = zeros (n - f(m), 1);
  57.     endif
  58.   else
  59.     r = [];
  60.   endif
  61.  
  62. endfunction
  63.